﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using WeiboFilter.Models;
using WeiboFilter.DAL;

namespace WeiboFilter.SinaWeibo
{
    /// <summary>
    /// import Sina data to DB
    /// </summary>
    public class GetDataFromSina
    {
        private const string basic_url = "http://api.t.sina.com.cn/";
        private const string sub_getloginuser = "account/verify_credentials.xml";//验证并得到个人信息
        private const string sub_getStatuses = "statuses/friends_timeline.xml";
        private const string sub_getUserStatuses = "statuses/user_timeline.xml";

        private string _token;
        private string _tokenSecret;

        public GetDataFromSina(string _token, string _tokenSecret)
        {
            this._token = _token;
            this._tokenSecret = _tokenSecret;
        }

        private User getUser(XmlNode node)
        {
            User u = new User();
            u.id = node.SelectSingleNode("id").InnerText;
            u.screen_name = node.SelectSingleNode("screen_name").InnerText;
            u.profile_image_url = node.SelectSingleNode("profile_image_url").InnerText;
            u.description = node.SelectSingleNode("description").InnerText;
            UserDAL uD = new UserDAL();
            uD.InsertWeiBoUser(u);
            return u;
        }


        private Status getStatus(XmlNode node,string customerId)
        {
            Status s = new Status();
            s.author = getUser(node.SelectSingleNode("user"));
            //insert the user info in DB first
            s.id = node.SelectSingleNode("id").InnerText;
            s.text = node.SelectSingleNode("text").InnerText;
            s.created_at =CommanHelper.ParseDateTime(node.SelectSingleNode("created_at").InnerText);
            StatusDAL sD = new StatusDAL();
            sD.InsertWeiBoStatus(s,customerId);
            return s;
        }

        /// <summary>
        /// return the count of statuses which have been imported in DB 
        /// </summary>
        /// <param name="since_id"></param>
        /// <returns></returns>
        public int getStatuses(ref string since_id/*每次都要since_id更新，保证每次数据不同*/,string customerId)
        {
            string param = string.Format("?count={0}&since_id={1}",200,since_id);
            oAuthSina oauth = new oAuthSina();
            oauth.token = _token;
            oauth.tokenSecret = _tokenSecret;

            string url = basic_url + sub_getStatuses+param;
            string xml = oauth.oAuthWebRequest(oAuthSina.Method.GET, url, string.Empty);

            XmlDocument xDom = new XmlDocument();
            xDom.LoadXml(xml);
            XmlNodeList nodelist=xDom.GetElementsByTagName("status");
            if (nodelist.Count == 0) return 0;
            since_id = xDom.SelectSingleNode("statuses/status/id").InnerText;
            foreach(XmlNode node in nodelist )
            {
                //need change here
                Status s = getStatus(node,customerId);
            }
            return nodelist.Count;
        }

        public User getLoginUser()
        {
            string url =basic_url  + sub_getloginuser;
            oAuthSina oauth = new oAuthSina();
            oauth.token = _token;
            oauth.tokenSecret = _tokenSecret;
            string xml = oauth.oAuthWebRequest(oAuthSina.Method.GET, url, string.Empty);
            XmlDocument xDom = new XmlDocument();
            xDom.LoadXml(xml);
            return getUser(xDom.SelectSingleNode("user"));
        }

        public string getUserStatuses(string max_id)
        {
            string result = string.Empty;

            string url = basic_url + sub_getUserStatuses + "?count=200";

            oAuthSina oauth = new oAuthSina();
            oauth.token = _token;
            oauth.tokenSecret = _tokenSecret;
            result = oauth.oAuthWebRequest(oAuthSina.Method.GET, url, string.Empty);

            return result;
        }



    }
}